package com.ssy.lingxi.search.controller.pc;

import com.ssy.lingxi.common.controller.BaseController;
import com.ssy.lingxi.common.exception.BusinessException;
import com.ssy.lingxi.common.model.dto.UserLoginCacheDTO;
import com.ssy.lingxi.common.response.PageData;
import com.ssy.lingxi.common.response.ResponseCode;
import com.ssy.lingxi.common.response.Wrapper;
import com.ssy.lingxi.search.api.model.request.CommoditySearchRequest;
import com.ssy.lingxi.search.api.model.request.SelfShopSearchCustomerRequest;
import com.ssy.lingxi.search.api.model.request.SelfShopSearchRequest;
import com.ssy.lingxi.search.api.model.response.*;
import com.ssy.lingxi.search.service.ICommodityService;
import com.ssy.lingxi.search.service.ISelfService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @menu: 自营商城
 * @author jw.chen
 * @version 2.0.0
 * @date 2021/10/15
 */
@RestController
@RequestMapping("/search/shop/self")
public class SelfController extends BaseController {

    @Resource
    private ICommodityService commodityService;

    @Resource
    private ISelfService selfService;

    /**
     * 查询商品会员品类树
     * @param memberId 会员id
     * @return 商品会员品类树
     */
    @RequestMapping(value = "/getCustomerCategoryTree", method = RequestMethod.GET)
    public Wrapper<List<CategoryTreeBrandResponse>> getCustomerCategoryTree(@RequestParam(value = "memberId") Long memberId) {
        Long shopId = this.getHeadersShopId();
        return Wrapper.success(selfService.getCategoryTreeAndBrand(shopId, memberId));
    }

    /**
     * 查询商品属性
     * @param memberId 会员id
     * @param categoryId 品类id
     * @return 商品属性
     */
    @RequestMapping(value = "/getCustomerAttributeByCategoryId", method = RequestMethod.GET)
    public Wrapper<List<AttributeResponse>> getCustomerAttributeByCategoryId(@RequestParam(value = "memberId") Long memberId, @RequestParam(value = "categoryId") Long categoryId) {
        Long shopId = this.getHeadersShopId();
        return Wrapper.success(selfService.getAttributeByCategoryId(shopId, memberId, categoryId));
    }

    /**
     * 查询商品品牌
     * @param memberId 会员id
     * @return 商品品牌
     */
    @RequestMapping(value = "/getBrand", method = RequestMethod.GET)
    public Wrapper<List<BrandResponse>> getBrand(@RequestParam(value = "memberId") Long memberId) {
        Long shopId = this.getHeadersShopId();
        return Wrapper.success(selfService.getBrand(shopId, memberId));
    }

    /**
     * 查询归属地市
     * @param memberId 会员id
     * @return 归属地市
     */
    @RequestMapping(value = "/getArea", method = RequestMethod.GET)
    public Wrapper<List<ProvinceResponse>> getArea(@RequestParam(value = "memberId") Long memberId) {
        Long shopId = this.getHeadersShopId();
        return Wrapper.success(selfService.getArea(shopId, memberId));
    }

    /**
     * 查询商品最大价格(积分)
     * @param memberId 会员id
     * @return 商品最大价格(积分)
     */
    @RequestMapping(value = "/getCommodityMaxPrice", method = RequestMethod.GET)
    public Wrapper<Double> getCommodityMaxPrice(@RequestParam(value = "memberId", required = false) Long memberId) {
        Long shopId = this.getHeadersShopId();
        return Wrapper.success(selfService.getCommodityMaxPrice(shopId, memberId));
    }

    /**
     * 查询商品列表
     * @param selfShopSearchRequest 查询条件实体
     * @return 商品列表
     */
    @RequestMapping(value = "/getCommodityList", method = RequestMethod.POST)
    public Wrapper<PageData<CommoditySearchResponse>> searchCommodityList(@RequestBody SelfShopSearchRequest selfShopSearchRequest) {
        Long shopId = this.getHeadersShopId();
        Long memberId = selfShopSearchRequest.getMemberId();
        if(memberId != null && memberId > 0){
            CommoditySearchRequest commoditySearchRequest = this.modelMapper.map(selfShopSearchRequest, CommoditySearchRequest.class);
            commoditySearchRequest.setShopId(shopId);
            //判断是否已登录
            if(this.isLogin()){
                UserLoginCacheDTO sysUser = this.getSysUser();
                commoditySearchRequest.setLoginMemberId(sysUser.getMemberId());
                commoditySearchRequest.setLoginMemberRoleId(sysUser.getMemberRoleId());
            }
            return Wrapper.success(commodityService.searchCommodityList(commoditySearchRequest, true, false));
        }else{
            throw new BusinessException(ResponseCode.SEARCH_MEMBER_NOT_EXIST);
        }
    }

    /**
     * 查询商品列表(积分商城)
     * @param selfShopSearchRequest 查询条件实体
     * @return 商品列表
     */
    @RequestMapping(value = "/getScoreCommodityList", method = RequestMethod.POST)
    public Wrapper<PageData<CommoditySearchResponse>> getScoreCommodityList(@RequestBody SelfShopSearchRequest selfShopSearchRequest) {
        Long shopId = this.getHeadersShopId();
        Long memberId = selfShopSearchRequest.getMemberId();
        if(memberId != null && memberId > 0){
            CommoditySearchRequest commoditySearchRequest = this.modelMapper.map(selfShopSearchRequest, CommoditySearchRequest.class);
            commoditySearchRequest.setShopId(shopId);
            //判断是否已登录
            if(this.isLogin()){
                UserLoginCacheDTO sysUser = this.getSysUser();
                commoditySearchRequest.setLoginMemberId(sysUser.getMemberId());
                commoditySearchRequest.setLoginMemberRoleId(sysUser.getMemberRoleId());
            }
            return Wrapper.success(commodityService.searchCommodityList(commoditySearchRequest, true, true));
        }else{
            throw new BusinessException(ResponseCode.SEARCH_MEMBER_NOT_EXIST);
        }
    }

    /**
     * 查询商品列表--代客下单
     * @param selfShopSearchCustomerRequest 查询条件实体
     * @return 商品列表
     */
    @RequestMapping(value = "/getCustomerCommodityList", method = RequestMethod.POST)
    public Wrapper<PageData<CommoditySearchResponse>> getCustomerCommodityList(@RequestBody SelfShopSearchCustomerRequest selfShopSearchCustomerRequest) {
        Long shopId = this.getHeadersShopId();
        Long memberId = selfShopSearchCustomerRequest.getMemberId();
        if(memberId != null && memberId > 0){
            CommoditySearchRequest commoditySearchRequest = this.modelMapper.map(selfShopSearchCustomerRequest, CommoditySearchRequest.class);
            commoditySearchRequest.setShopId(shopId);
            commoditySearchRequest.setLoginMemberId(selfShopSearchCustomerRequest.getCustomerMemberId());
            commoditySearchRequest.setLoginMemberRoleId(selfShopSearchCustomerRequest.getCustomerMemberRoleId());
            return Wrapper.success(commodityService.searchCommodityList(commoditySearchRequest, true, false));
        }else{
            throw new BusinessException(ResponseCode.SEARCH_MEMBER_NOT_EXIST);
        }
    }

    /**
     * 查询商品列表(积分商城)--代客下单
     * @param selfShopSearchCustomerRequest 查询条件实体
     * @return 商品列表
     */
    @RequestMapping(value = "/getCustomerScoreCommodityList", method = RequestMethod.POST)
    public Wrapper<PageData<CommoditySearchResponse>> getCustomerScoreCommodityList(@RequestBody SelfShopSearchCustomerRequest selfShopSearchCustomerRequest) {
        Long shopId = this.getHeadersShopId();
        Long memberId = selfShopSearchCustomerRequest.getMemberId();
        if(memberId != null && memberId > 0){
            CommoditySearchRequest commoditySearchRequest = this.modelMapper.map(selfShopSearchCustomerRequest, CommoditySearchRequest.class);
            commoditySearchRequest.setShopId(shopId);
            commoditySearchRequest.setLoginMemberId(selfShopSearchCustomerRequest.getCustomerMemberId());
            commoditySearchRequest.setLoginMemberRoleId(selfShopSearchCustomerRequest.getCustomerMemberRoleId());
            return Wrapper.success(commodityService.searchCommodityList(commoditySearchRequest, true, true));
        }else{
            throw new BusinessException(ResponseCode.SEARCH_MEMBER_NOT_EXIST);
        }
    }
}
